package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.anhuanjia.tools.eao.bean.EaoProductionEquipment;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.utility.StringUtil;

/**
 *
 * @ClassName: ProductEquipmentUI
 * @Description: 生产设备情况表的编辑
 * @author wangqiangqiang
 * @date 2018年9月18日上午11:01:14
 * 
 */

@Alias(value = "productEquipment")
public class ProductEquipmentUI extends UIFacade {
	/**
	 * 
	 * @Title: saveOrUpdateProductEquipment
	 * @Description: 生产设备情况添加或修改
	 * @author wangqiangqiang
	 * @date 2018年9月18日上午11:05:20
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdateProductEquipment() {
		String productEquipmentList = $V("ProductEquipmentList");
		if (StringUtil.isNotEmpty(productEquipmentList)) {
			Transaction transaction = new Transaction();
			List<Map<String, Object>> array = JSON.parseObject(productEquipmentList,
					new TypeReference<List<Map<String, Object>>>() {
					});
			List<String> peids = new ArrayList<>();
			String project_id = null;
			for (Map<String, Object> list : array) {
				System.out.println(list);
				EaoProductionEquipment editEquipment = new EaoProductionEquipment();
				editEquipment.setValue(list);
				project_id = editEquipment.getProjectId();
				String id = editEquipment.getPeId();
				if (StringUtils.isEmpty(id)) {
					// 添加
					editEquipment.setPeId(UUID.randomUUID().toString());
					transaction.insert(editEquipment);
					peids.add(editEquipment.getPeId());
				} else {
					// 更新
					peids.add(id);
					DataTable dt = new Q(" select * from eao_production_equipment where PeId = ?", id).fetch();
					EaoProductionEquipment entity = new EaoProductionEquipment();
					entity.setValue(dt.get(0));
					entity.setValue(list);
					transaction.update(entity);
				}
			}
			DataTable dt = new Q(" select * from eao_production_equipment where ProjectId = ?", project_id).fetch();
			for (DataRow dataRow : dt) {
				EaoProductionEquipment editEquipment = new EaoProductionEquipment();
				editEquipment.setValue(dataRow);
				Boolean del = true;
				for (String peid : peids) {
					if (editEquipment.getPeId().equals(peid)) {
						del = false;
					}
				}
				if (del) {
					transaction.delete(editEquipment);
				}
			}
			if (transaction.commit()) {
				s("操作成功");
			} else {
				f("操作失败");
			}
		} else {
			this.Response.put("data", "无操作记录");
		}
	}

//	/**
//	 * 
//	 * @Title: findAllProductionEquipment
//	 * @Description: 根据(项目id)查询所有生产设备情况
//	 * @author wangqiangqiang
//	 * @date 2018年9月18日上午11:17:40
//	 */
//	@Priv(loginType = LoginType.Member)
//	public void findAllProductionEquipment() {
//		String project_id = $V("ProjectId");
//		DataTable productionEquipmentDt = new Q(" select * from eao_production_equipment where ProjectId =? ",
//				project_id).fetch();
//		this.Response.put("productionEquipmentDt", productionEquipmentDt);
//	}

	/**
	 * 
	 * @Title: deleteProductionEquipmentByID
	 * @Description: 根据id删除单个生产设备情况
	 * @author wangqiangqiang
	 * @date 2018年9月18日上午11:20:11
	 */
	@Priv(loginType = LoginType.Member)
	public void deleteProductionEquipmentByID() {
		String id = $V("Id");
		if (StringUtil.isNotEmpty(id)) {
			Transaction transaction = new Transaction();
			DataTable deleteProductionEquipmentDt = new Q(" select * from eao_production_equipment where PeId = ?", id)
					.fetch();
			if (deleteProductionEquipmentDt.getRowCount() != 0) {
				EaoProductionEquipment deleteEquipment = new EaoProductionEquipment();
				deleteEquipment.setValue(deleteProductionEquipmentDt.get(0));
				transaction.delete(deleteEquipment);
				if (transaction.commit()) {
					s("删除生产情况成功");
				} else {
					f("删除生产情况失败");
				}
			}

		}

	}

//	/**
//	 * 根据项目ID查询其下的生产情况
//	 */
//	@Priv(loginType = LoginType.Member)
//	public void findByProjectId() {
//		String pid = $V("Id");
//		if (StringUtils.isEmpty(pid)) {
//			f("查询失败，请确认查询信息");
//			return;
//		}
//		DataTable dt = new Q("select * from eao_production_equipment where ProjectId = ? ", pid).fetch();
//		List<EaoProductionEquipment> list = new ArrayList<>();
//		for (DataRow dr : dt) {
//			EaoProductionEquipment entity = new EaoProductionEquipment();
//			entity.setValue(dr);
//			list.add(entity);
//			this.Response.put("data", list);
//		}
//	}
}
